package cn.demomaster.quickdb.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class TableAdapter extends RecyclerView.Adapter<TableAdapter.TableRowViewHolder> implements ITableAdapter {
    public List<String[]> data;
    public Context context;

    public TableAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public void updateList(List<String[]> data) {
        this.data.clear();
        this.data.addAll(data);
//        if (mRowAdapter != null) {
//            mRowAdapter.setData(data);
//        }
        notifyDataSetChanged();
    }

    //public TableRowAdapter mRowAdapter;

//    public void setRowAdapter(TableRowAdapter tableRowAdapter) {
//        this.mRowAdapter = tableRowAdapter;
//        mRowAdapter.setData(data);
//    }

    @Override
    public TableRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (mRowAdapter == null) {
//            mRowAdapter = new TableRowAdapter(context);
//            mRowAdapter.setData(data);
//        }
        ViewGroup rowLayout = getRowLayout(viewType);
        for (int i = 0; i < getColumnCount(); i++) {
            View rowView = getCellView(null, i);
            if(rowView.getParent()==null) {
                if (rowView.getLayoutParams() != null) {
                    rowLayout.addView(rowView, rowView.getLayoutParams());
                } else {
                    rowLayout.addView(rowView);
                }
            }
        }
        return new TableRowViewHolder(rowLayout);
    }

    @Override
    public void onBindViewHolder(TableRowViewHolder holder, int position) {
        ViewGroup rowLayout = (ViewGroup) holder.itemView;
        rowLayout.removeAllViews();
        for (int i = 0; i < getColumnCount(); i++) {
            View cellView = getCellView(rowLayout, i);
            if(cellView.getParent()==null) {
                if (cellView.getLayoutParams() != null) {
                    rowLayout.addView(cellView, cellView.getLayoutParams());//,new ViewGroup.LayoutParams(400, ViewGroup.LayoutParams.WRAP_CONTENT));
                } else {
                    rowLayout.addView(cellView);
                }
            }
        }
        holder.onBind(data.get(position), position);
        bindRowViewData(position, (ViewGroup) holder.itemView);
        for (int i = 0; i < ((ViewGroup) holder.itemView).getChildCount(); i++) {
            bindCellViewData(position, i, (ViewGroup) holder.itemView, ((ViewGroup) holder.itemView).getChildAt(i));
            //mRowAdapter.bindData(position,i,rowData);
        }
    }

    @Override
    public int getItemCount() {
        return getRowCount();
    }
    @Override
    public int getRowCount() {
        return data == null ? 0 : data.size();
    }
    @Override
    public int getColumnCount() {
        return (data == null || data.isEmpty()) ? 0 : data.get(0).length;
    }
//    public View getTitleView(int position) {
//        ViewGroup headerLayout = (ViewGroup) mRowAdapter.getHeaderLayout();
//        for(int i = 0; i < mRowAdapter.getColumnCount(); i++){
//           View cellView = mRowAdapter.getCellView(headerLayout,i);
//            headerLayout.addView(cellView);
//        }
//        return headerLayout;
//    }

    public static class TableRowViewHolder extends RecyclerView.ViewHolder {
        public TableRowViewHolder(View itemView) {
            super(itemView);
        }

        public void onBind(String[] rowData, final int position) {
//            bindRowViewData(position, (ViewGroup) itemView);
//            for (int i = 0; i < ((ViewGroup) itemView).getChildCount(); i++) {
//                bindCellViewData(position, i, (ViewGroup) itemView, ((ViewGroup) itemView).getChildAt(i));
//                //mRowAdapter.bindData(position,i,rowData);
//            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void bindRowViewData(int rowIndex, ViewGroup rowlayout) {
        if (rowIndex == -1) {
            rowlayout.setBackgroundColor(Color.BLUE);
        } else {
            rowlayout.setBackgroundColor(rowIndex % 2 == 0 ? Color.GRAY : Color.WHITE);
        }
    }
}